<template lang="pug">
	.get-with-card-wrap
		.top-tip
			top-tip( tip="请认真填写您的姓名、身份证号，便于您使用预约挂号等医疗服务。信息提交后无法修改")
		.card-input-container(v-if="familyRelation!=='本人'")
			picker(@change="changeRelation" :value="relationIndex" :range="relation")
				input-line(leftText='家庭关系' placeholder="请选择家庭关系" type="text" :disabled='true' :showArrow="true" :showBorder="false" ref="familyRelation")
		.card-input-container
				input-line(leftText='姓名' placeholder="请输入真实姓名" type="text" @getValue="getName" :disabled="nameDisabled" ref="name")
				input-line(leftText='身份证号' placeholder="请输入身份证号码" type="idcard" @getValue="getIdNumber" maxlength="18" :showBorder="false" :disabled="idNumberDisabled" ref="idNumber")
		.card-input-container
				input-line(leftText='就诊手机号' placeholder="用于接收就诊短信，请认真填写" type="number" @getValue="getPhone" maxlength="11" :showBorder="false")
		.card-btn(@click="jumpToNext")
			main-button(text="确认身份信息" :isActive="nextStepPass")
</template>

<script>
import inputLine from '@/components/input-line'
import mainButton from '@/components/main-button'
import topTip from '@/components/top-tip'
export default {
	name: 'insertId',
	data() {
		return {
			name: '',
			idNumber: '',
			phone: '',
			familyRelation: '',
			relationIndex: -1,
			relation: [],
			nextStepPassRelation: false,
			nextStepPassName: false,
			nextStepPassIdNumber: false,
			nextStepPassPhone: false,
			nameDisabled: false,
			idNumberDisabled: false,
			hasIdFace: false //判断省厅有无人脸
		}
	},
	computed: {
		nextStepPass() {
			return (
				this.nextStepPassRelation &&
				this.nextStepPassName &&
				this.nextStepPassIdNumber &&
				this.nextStepPassPhone
			)
		}
	},
	created() {},
	onLoad() {
		Object.assign(this.$data, this.$options.data())
	},
	mounted() {
		// 1. 新增家人
		// 2. 传入关系是本人
		// 3. 传入关系非本人
		this.relation = ['老公', '老婆', '爸爸', '妈妈', '儿子', '女儿', '其他']

		if (this.$route.query.isReadFamily) {
			let familyRole = this.wx.getStorageSync('familyRole')
			this.name = familyRole.name
			this.idNumber = familyRole.idNumber
			this.familyRelation = familyRole.familyRelation
			if (this.familyRelation !== '本人') {
				//判断关系
				for (let i in this.relation) {
					if (this.relation[i] === this.familyRelation) {
						this.relationIndex = i
						this.$refs.familyRelation.inputtext = this.familyRelation
						this.nextStepPassRelation = true
						break
					}
				}
			} else {
				this.nextStepPassRelation = true
			}

			this.nameDisabled = this.name.length > 0
			this.nextStepPassName = this.name.length > 0
			this.$refs.name.inputtext = this.name

			this.idNumberDisabled = this.tools.identityCodeValid(this.idNumber)
			this.nextStepPassIdNumber = this.tools.identityCodeValid(this.idNumber)
			this.$refs.idNumber.inputtext = this.idNumber
		}
	},
	methods: {
		jumpToNext() {
			if (this.nextStepPass) {
				this.submit()
			}
		},
		changeRelation(e) {
			this.relationIndex = e.mp.detail.value
			this.$refs.familyRelation.inputtext = this.relation[e.mp.detail.value]
			this.nextStepPassRelation = true
		},
		getName(text) {
			text = text.toString()
			this.name = text
			this.nextStepPassName = text.length > 0
		},
		getIdNumber(text) {
			text = text.toString()
			this.idNumber = text
			if (text.length === 15 || text.length === 18) {
				this.nextStepPassIdNumber = this.tools.identityCodeValid(text)
			} else {
				this.nextStepPassIdNumber = false
			}
		},
		getPhone(text) {
			text = text.toString()
			this.phone = text
			this.nextStepPassPhone =
				text.length === 11 ? this.tools.isMobile(text) : false
		},
		getAge() {
			let birth,
				idNumber = this.idNumber
			if (idNumber.length === 15) {
				birth = '19' + idNumber.substr(6, 6)
			} else {
				birth = idNumber.substr(6, 8)
			}
			birth = this.dayjs(birth.replace(/(.{4})(.{2})/, '$1/$2/'))
			return this.dayjs(new Date()).diff(birth, 'year')
		},
		getProvinceFace() {
			this.api.tzApi
				.getProvinceFace({
					name: this.name,
					idNumber: this.idNumber
				})
				.then(data => {
					data.responseFlag === '1'
						? this.$router.push({
								path: '/views/faceAuthentication/faceDetection',
								query: {
									mode: 'oneFace'
								}
						  })
						: this.$router.push({
								path: '/views/faceAuthentication/uploadIdPhoto'
						  })
				})
				.catch(err => {
					this.$router.push({
						path: '/views/faceAuthentication/uploadIdPhoto'
					})
				})
		},
		isExist() {
			let that = this
			this.api.tzApi
				.isExist({
					name: this.name,
					idNumber: this.idNumber
				})
				.then(data => {
					if (data.responseFlag === '1') {
						if (data.data.isExist === '1') {
							//身份证对应用户已存在
							if (data.data.phoneNumber !== this.phone) {
								this.wx.showModal({
									title: '提示',
									content: `该家人已经绑定手机号${this.tools.hideNum(
										data.data.phoneNumber,
										3,
										4
									)},成功添加家人后可修改`,
									confirmText: '确定',
									success(res) {
										if (res.confirm) {
											that.phone = data.data.phoneNumber
											that.$refs.phone.inputtext = data.data.phoneNumber
											that.wx.setStorage({
												key: 'familyRole',
												data: Object.assign(
													that.wx.getStorageSync('familyRole'),
													{ phone: that.phone }
												)
											})
										}
									}
								})
							}
							that.$router.push({
								path: '/views/faceAuthentication/faceDetection',
								query: {
									mode: 'oneFace'
								}
							})
						} else {
							//对应用户不存在
							//判断是否成年
							this.getAge() < 18
								? this.$router.push({
										path: '/views/faceAuthentication/selectParent'
								  })
								: this.getProvinceFace()
						}
					}
				})
				.catch(() => {})
		},
		async submit() {
			if (this.familyRelation === '本人') {
				//认证本人时判断账号是否被他人绑定为本人
				await this.api.tzApi
					.isBindME({
						name: this.name,
						idNumber: this.idNumber
					})
					.then(data => {
						if (data.responseFlag === '1') {
							if (data.data.isBind === '1') {
								this.wx.showToast({
									title: data.responseMessage,
									icon: 'none'
								})
								return false
							}
						}
					})
					.catch(() => {})
			}
			this.isExist()

			//人脸认证，提交卡管系统
			// let params = {
			// 	title: '领取成功',
			// 	info: '您现在可以使用电子健康卡进行预约挂号、缴费等医疗服务',
			// 	button: [
			// 		{
			// 			text: '绑定社保卡',
			// 			action: 'bind',
			// 			white: false
			// 		},
			// 		{
			// 			text: '完成',
			// 			action: 'done',
			// 			white: true
			// 		}
			// 	]
			// }
			// this.wx.setStorage({
			// 	key: 'actionResult',
			// 	data: params
			// })
			// this.$router.push({
			// 	path: '/views/healthCard/actionResult',
			// 	query: {
			// 		name: this.atob(this.name)
			// 	}
			// })
		}
	},
	components: {
		topTip,
		inputLine,
		mainButton
	}
}
</script>

<style lang="stylus">
	.get-with-card-wrap
		.top-tip
			padding mainPadding
		.card-input-container
			margin-bottom mainMargin
			background-color white
			.input-line-wrap
				margin 0
				padding 0 40px
		.card-btn
			width 690px
			height 92px
			margin 60px auto 0
			border-radius 46px
			overflow hidden
</style>
